<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物车</title>
<script>
/*
*声明一个数组用来存放购物信息
*/
var buies = new Array();
/*
*购买商品
*/
function buyPhone(id,name) {
	var buy = {};//new Object()
	buy.id=id;//设置商品编号
	buy.name=name;//设置商品名称
	buy.count=1;//设置商品数量
	addBuy(buy);//将商品添加到数组中
	showBuies();//显示商品
}
/*
*显示商品
*/
function showBuies() {
	$("#myBuies").empty();//清空显示列表
	var htm="<tr><td>序号</td><td>商品名称</td><td>数量</td><td>操作</td></tr>";
	$("#myBuies").append(htm);
	for(i=0; i<buies.length; i++){		
		var hh = "<tr>"+
					"<td>"+(i+1)+
					"</td>"+
					"<td>"+buies[i].name+
					"</td>"+
					"<td> <a onClick=add('"+buies[i].id+"',1)>  +  </a>"+buies[i].count+
					"  <a onClick=less('"+buies[i].id+"','1')>  -  </a></td>"+
					"<td>"+" <a onClick=less('"+buies[i].id+"','-')>删除</a>"+
					"</td>"+
				"</tr>";
		$("#myBuies").append(hh);
	}
}
/*
*添加商品
*/
function addBuy(buy) {
	for(i=0;i<buies.length;i++){
		//判断数组中是否存在同样的商品
		if(buy.id==buies[i].id){
			buy.count+=buies[i].count;
			buies[i]=buy;
			return;
		}
	}
	//不存在 直接添加
	buies[buies.length]=buy;
}
/*
*减少商品数量
*可以指定减少的数额
*/
function less(id,count) {
	for(i=0;i<buies.length;i++){
		if(buies[i].id==id){
			if(count=="-"){
				buies.remove(i);
			}
			else if(buies[i].count-count>=1){
				var buy = buies[i];
				buy.count-=count;
				buies[i]=buy;
			}
			else {
				buies.remove(i);
			}
		}
	}
	//显示商品
	showBuies();
}
/*
*"+" 按钮事件，可以指定添加的数量
*/
function add(id,count) {
	if(count < 0){return false;}//检查数字是否正确
		for(i=0;i<buies.length;i++){
			if(buies[i].id==id){
				var buy = buies[i];
				buy.count+=count;
				buies[i]=buy;
			}
	}
	showBuies();
}
/*
*根据下标删除数组中的元素
*/
 Array.prototype.remove=function(dx)
  {
    if(isNaN(dx)||dx>this.length){return false;}
    for(var i=0,n=0;i<this.length;i++)
    {
        if(this[i]!=this[dx])
        {
            this[n++]=this[i]
        }
    }
    this.length-=1
  }

</script>
<script>
 $(function(){
 $("#shopingForm").submit(function(){ 
  for(i=0; i<buies.length; i++){		
		var hh = "<input type='hidden' name='orders["+i+"].id' value='"+buies[i].id+"' />";
		hh=hh+"<input type='hidden' name='orders["+i+"].amount' value='"+buies[i].count+"' />";
		$("#shop").append(hh);
	}
  })
 })
</script>
</head>

<body>
<table>
<tr>
				<td nowrap="nowrap">序号&nbsp;</td>
				<td nowrap="nowrap">餐厅&nbsp;</td>
				<td nowrap="nowrap">菜名&nbsp;</td>
				<td nowrap="nowrap">单价&nbsp;</td>
				<td nowrap="nowrap">服务口号&nbsp;</td>
				<td nowrap="nowrap">简介&nbsp;</td>
				<td nowrap="nowrap">状态&nbsp;</td>
				<td nowrap="nowrap">操作&nbsp;</td>
		</tr>
<#if pageProduct??>
<#list pageProduct.result as list>
    	<tr>
				<td nowrap="nowrap">${page.getFirst()+list_index}&nbsp;</td>
				<td nowrap="nowrap">${list.supplier.name?default("")}&nbsp;</td>
				<td nowrap="nowrap">${list.variety.name?default("")}&nbsp;</td>
				<td nowrap="nowrap">${list.price?default("")}&nbsp;</td>
				<td nowrap="nowrap">${list.slogan?default("")}&nbsp;</td>
				<td nowrap="nowrap">
				${list.introdution?default("")}&nbsp;
				</td>
				<td nowrap="nowrap">
				${list.state?default("")}&nbsp;
				</td>
				<td nowrap="nowrap">
				<a href="#" onClick="buyPhone('${list.id?default("")}','${list.variety.name?default("")}')">我要定&nbsp;</a>
				</td>
		</tr>
    </#list>
    </#if>
</table>
<table border="1" id="myBuies">
<tr><td>序号</td><td>商品名称</td><td>数量</td><td>操作</td></tr>
</table>
	<form name="shopingForm" id="shopingForm" method="post" action="${base}/m/order/save.shtml">
	<input type="hidden" name="order.id" id="order.id" value="${order.id?default('')}" />
	<input type="hidden" name="order.version" id="order.version" value="${order.version?default('')}" />
	<br>
	订单人姓名：<input type="text" name="order.fromName" id="order.fromName" value="${order.fromName?default('')}" /><br>
	订单人地址：<input type="text" name="order.fromAddress" id="order.fromAddress" value="${order.fromAddress?default('')}" /><br>
	订单人电话：<input type="text" name="order.fromTelphone" id="order.fromTelphone" value="${order.fromTelphone?default('')}" /><br>
	<div id="shop"></div>
	  <input type="submit" name="button" id="update" value="提交" />
	</form>
</body>
</html> 
